/**
 * RuleAction.java
 */
package com.prs.crm.action.document.rule;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.doc.UploadDoc;
import com.prs.crm.domain.document.knowledge.Doc;
import com.prs.crm.domain.document.rule.Rule;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.sys.DocumentAdmin;
import com.prs.crm.domain.sys.FileTypeMaintain;
import com.prs.crm.domain.warning.WarnContent;
import com.prs.crm.service.document.rule.RuleService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.sys.DocumentAdminService;
import com.prs.crm.service.sys.FileTypeMaintainService;
import com.prs.crm.service.warning.WarnContentService;
import com.prs.crm.tools.ControlJMail;
import com.prs.crm.tools.Datetools;
import com.prs.crm.tools.NoteTools;
import com.prs.defaultTools.PrsToolKit;

/**
 * @author ZhuDongMei
 * @E-mail:1223984339@qq.com
 * @qq:1223984339
 * @version 创建时间：2011-3-16 上午11:12:28 简单说明
 */

public class RuleAction extends BaseAction {
	static final int BUFFER = 1024;
	private static final long serialVersionUID = 1L;
	private Rule rule;
	private PaginationSupport pagination;
	private RuleService ruleService;
	private Collection<TreeNode> nodes;
	private File upload;
	private File uplodS;
	private String uploadFileName;
	private String uplodSFileName;
	private PrsToolKit prsToolKit;
	private String ruleIds;
	private FileTypeMaintainService fileTypeMaintainService;
	private ControlJMail cj=new ControlJMail();
	private NoteTools nt=new NoteTools();
	private Datetools dt=new Datetools();
	private PersonService personService;
	private WarnContentService warnContentService;
	private DocumentAdminService documentAdminService;
	public DocumentAdminService getDocumentAdminService() {
		return documentAdminService;
	}

	public void setDocumentAdminService(DocumentAdminService documentAdminService) {
		this.documentAdminService = documentAdminService;
	}

	public ControlJMail getCj() {
		return cj;
	}

	public void setCj(ControlJMail cj) {
		this.cj = cj;
	}

	public NoteTools getNt() {
		return nt;
	}

	public void setNt(NoteTools nt) {
		this.nt = nt;
	}

	public Datetools getDt() {
		return dt;
	}

	public void setDt(Datetools dt) {
		this.dt = dt;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public WarnContentService getWarnContentService() {
		return warnContentService;
	}

	public void setWarnContentService(WarnContentService warnContentService) {
		this.warnContentService = warnContentService;
	}

	public FileTypeMaintainService getFileTypeMaintainService() {
		return fileTypeMaintainService;
	}

	public void setFileTypeMaintainService(
			FileTypeMaintainService fileTypeMaintainService) {
		this.fileTypeMaintainService = fileTypeMaintainService;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUplodS() {
		return uplodS;
	}

	public void setUplodS(File uplodS) {
		this.uplodS = uplodS;
	}

	public String getUplodSFileName() {
		return uplodSFileName;
	}

	public void setUplodSFileName(String uplodSFileName) {
		this.uplodSFileName = uplodSFileName;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public PrsToolKit getPrsToolKit() {
		return prsToolKit;
	}

	public void setPrsToolKit(PrsToolKit prsToolKit) {
		this.prsToolKit = prsToolKit;
	}

	public String getRuleIds() {
		return ruleIds;
	}

	public void setRuleIds(String ruleIds) {
		this.ruleIds = ruleIds;
	}

	public Rule getRule() {
		return rule;
	}

	public void setRule(Rule rule) {
		this.rule = rule;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public RuleService getRuleService() {
		return ruleService;
	}

	public void setRuleService(RuleService ruleService) {
		this.ruleService = ruleService;
	}

	@Action("listRule")
	public String listRule() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<Rule> d=this.getRuleService().getAllRules();
		for (Rule ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getRuleService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getRuleService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getRuleService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Rule.class).add(Restrictions.isNull("parent"));
		criteria.add(Restrictions.eq("viState", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRuleService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "loadRuleTrees", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadRuleTrees() {
		nodes = new ArrayList<TreeNode>();
		Collection<Rule> rules = this.getRuleService().getRootRule();
		for (Rule rule : rules) {
			if (rule.getUpState()==1) {
				TreeNode node = new TreeNode();
				node.setId(String.valueOf(rule.getId()));
				node.setText(rule.getName());
				addChildNodes(rule, node);
				nodes.add(node);
			}
		}
		return SUCCESS;
	}

	private void addChildNodes(Rule rule, TreeNode parentnode) {
		if (rule.getChilds() != null) {
			for (Rule r : rule.getChilds()) {
				if (r.getType().equals("类别")) {
					if (r.getUpState()==1) {
					TreeNode node = new TreeNode();
					node.setId(String.valueOf(r.getId()));
					node.setText(r.getName());
					parentnode.addChildNode(node);
					addChildNodes(r, node);
					}
				}
			}
		}
	}

	@Action(value = "loadRuleTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadRuleTree() {
		nodes = new ArrayList<TreeNode>();
		Collection<Rule> rules = this.getRuleService().getRootRule();
		for (Rule rule : rules) {
			TreeNode node = new TreeNode();
			node.setId(String.valueOf(rule.getId()));
			node.setText(rule.getName());
			addChildNode(rule, node);
			nodes.add(node);
		}
		return SUCCESS;
	}

	private void addChildNode(Rule rule, TreeNode parentnode) {
		if (rule.getChilds() != null) {
			for (Rule r : rule.getChilds()) {
				if ("类别".equals(r.getType().trim())) {
					TreeNode node = new TreeNode();
					node.setId(String.valueOf(r.getId()));
					node.setText(r.getName());
					parentnode.addChildNode(node);
					addChildNode(r, node);
				}
			}
		}
	}

	@Action("listRuleTree")
	public String listRuleTree() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<Rule> d=this.getRuleService().getAllRules();
		for (Rule ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getRuleService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getRuleService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getRuleService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
		}
		return SUCCESS;
	}

	@Clew(results = { "error" })
	@Action(value = "removeRule", results = {
			@Result(name = "success", type = "redirect", location = "listRuleTree"),
			@Result(name = "error", type = "redirect", location = "listRuleTree") })
	public String removeRule() {
		if (rule.getChilds().size() != 0) {
			this.setPrompt("该文件下面有关联文件，不能删除该文件！");
			return ERROR;
		} else {
			this.getRuleService().removeRule(rule.getId());
			return SUCCESS;
		}
	}
	@Clew(results = { "error" })
	@Action(value = "removeRules", results = {
			@Result(name = "success", type = "redirect", location = "listRule"),
			@Result(name = "error", type = "redirect", location = "listRule") })
	public String removeRules() {
		if (rule.getChilds().size() != 0) {
			this.setPrompt("该文件下面有关联文件，不能删除该文件！");
			return ERROR;
		} else {
			this.getRuleService().removeRule(rule.getId());
			return SUCCESS;
		}
	}
	@Action("listRu")
	public String listRu() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<Rule> d=this.getRuleService().getAllRules();
		for (Rule ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getRuleService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getRuleService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getRuleService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Rule.class).add(Restrictions.isNull("parent"));
		criteria.add(Restrictions.eq("viState", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRuleService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listRuleByCategory")
	public String listRuleByCategory() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<Rule> d=this.getRuleService().getAllRules();
		for (Rule ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getRuleService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getRuleService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getRuleService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Rule.class).add(Restrictions.isNull("parent"));
		criteria.add(Restrictions.eq("viState", 1));
		// criteria.add(Restrictions.in("parent",this.getRuleService().convertToRule(ruleIds)));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRuleService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listRuleByCategorys")
	public String listRuleByCategorys() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<Rule> d=this.getRuleService().getAllRules();
		for (Rule ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getRuleService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getRuleService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getRuleService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Rule.class).add(Restrictions.isNotNull("parent")).add(
				Restrictions.eq("parent", rule));
		criteria.add(Restrictions.eq("viState", 1));
		// criteria.add(Restrictions.in("parent",this.getRuleService().convertToRule(ruleIds)));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRuleService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editCategory")
	public String editCategory() {
		return SUCCESS;
	}

	@Action("listRules")
	public String listRules() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<Rule> d=this.getRuleService().getAllRules();
		for (Rule ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getRuleService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getRuleService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getRuleService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Rule.class).add(
				Restrictions.eq("parent", this.getRuleService().getRuleById(
						rule.getId())));
		criteria.add(Restrictions.eq("viState", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRuleService().listByPage(criteria,
				pagination));
		return SUCCESS;

	}

	@Action("listRulesTree")
	public String listRulesTree() {
		Person cuPerson=AppContext.getAccountContext().currentUser();
		List<Rule> d=this.getRuleService().getAllRules();
		for (Rule ob : d) {
			if (ob.getRecorder().getResume().getName().equals(cuPerson.getResume().getName())) {
				ob.setViState(1);//1表示有访问权限，2没有访问权限
				ob.setUpState(1);//1表示有修改权限，2没有修改权限
				this.getRuleService().submit(ob);
			}else{
				if (ob.getPersonId()==null || "".equals(ob.getPersonId().trim())) {
					ob.setUpState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getPersonId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setUpState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setUpState(2);
							this.getRuleService().submit(ob);
						}
					}	
				}
				if (ob.getVisitId()==null || "".equals(ob.getVisitId().trim())) {
					ob.setViState(1);
					this.getRuleService().submit(ob);
				}else{
					String[] per=ob.getVisitId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setViState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setViState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
			DocumentAdmin da=this.getDocumentAdminService().getDocumentAdmin("dm01");
			if (da!=null) {
				if (da.getPersonNameId()==null || "".equals(da.getPersonNameId().trim())) {
					ob.setDelState(2);
					this.getRuleService().submit(ob);
				}else{
					String[] per=da.getPersonNameId().split(",");
					for (String pers : per) {
						if (cuPerson.getId().equals(Integer.parseInt(pers))) {
							ob.setDelState(1);
							this.getRuleService().submit(ob);
							break;
						}else{
							ob.setDelState(2);
							this.getRuleService().submit(ob);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Rule.class).add(
				Restrictions.eq("parent", this.getRuleService().getRuleById(
						rule.getId())));
		criteria.add(Restrictions.eq("viState", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRuleService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "saveCategory", results = {
			@Result(name = "success", type = "redirect", location = "listRule"),
			@Result(name = "error", type = "chain", location = "editCategory") })
	public String saveCategory() {
		if ("".equals(rule.getName())) {
			this.addActionError("类别名称不能为空！");
			return ERROR;
		}
		rule.setTime(new Date());
		rule.setType("类别");
		rule.setUpPerson(AppContext.getAccountContext().currentUser());
		this.getRuleService().save(rule);
		if (rule.getId()!=null) {
			if ("给指定人员发邮件".equals(rule.getUpdateSet())) {
				String[] perId=rule.getAppiontPersonId().split(",");
				for (String per : perId) {
					Person person=this.getPersonService().getPersonById(Integer.parseInt(per));
					Person curper=AppContext.getAccountContext().getUser();
					if (person!=null) {
						cj.sendMail(curper.getResume().getEmail(), person.getResume().getEmail(), person.getLoginCode(), person.getLoginPwd(), "文件夹更新信息", "文件夹"+rule.getName()+"已更新");
					}
				}
			}
			if ("给指定人员发送手机短信".equals(rule.getUpdateSet())) {
				String[] perId=rule.getAppiontPersonId().split(",");
				for (String per : perId) {
					Person person=this.getPersonService().getPersonById(Integer.parseInt(per));
					if (person!=null) {
						Date date=new Date();
						SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
						try {
							nt.SendNote(person.getResume().getMobile_No(),"文件夹"+rule.getName()+"已更新",sf.format(date));
						} catch (MalformedURLException e) {
							e.printStackTrace();
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}	
					}
				}
			}
			if ("给指定人员发送内部消息".equals(rule.getUpdateSet())) {
				String[] perId=rule.getAppiontPersonId().split(",");
				for (String per : perId) {
					Person person=this.getPersonService().getPersonById(Integer.parseInt(per));
					Person curper=AppContext.getAccountContext().getUser();
					if (person!=null) {
						Date date=new Date();
						SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddHHmmss");
						 WarnContent wc=new WarnContent();	
				    	 wc.setWarnstyl("文件夹创建成功");
				    	 wc.setWarnCont("文件夹"+rule.getName()+"更新成功");
				    	 wc.setReceivID(per);
				    	 wc.setReadState("1");
					     this.getWarnContentService().save(wc);		
					}
				}
			}
		}
		return SUCCESS;
	}

	@Action("editFile")
	public String editFile() {
		return SUCCESS;
	}

	@Action(value = "saveFile", results = {
			@Result(name = "success", type = "redirect", location = "listRule"),
			@Result(name = "error", type = "chain", location = "editFile") })
	public String saveFile() {
		if ("".equals(rule.getName())) {
			this.addActionError("文件主题不能为空！");
			return ERROR;
		}
		rule.setTime(new Date());
		rule.setUpPerson(AppContext.getAccountContext().currentUser());
		this.getRuleService().save(rule);
		return SUCCESS;
	}

	@Action("editUpload")
	public String editUpload() {
		return SUCCESS;
	}

	@Action(value = "volumeUpload", results = {
			@Result(name = "success", type = "redirect", location = "listRule"),
			@Result(name = "error", type = "chain", location = "editVolumeUpload") })
	public String volumeUpload() throws IOException {
		if (uplodS != null) {
			String str = uplodSFileName.substring(uplodSFileName.length() - 3,
					uplodSFileName.length());
			if ("zip".equals(str)) {
				long size = new FileInputStream(new File(uplodS.getPath()))
						.available();
				if ("0".equals(AppContext.getAccountContext().currentUser()
						.getUploadMaxVolume())
						|| "".equals(AppContext.getAccountContext()
								.currentUser().getUploadMaxVolume())) {
					success();
					return SUCCESS;
				} else {
					long max = new Double(Double.parseDouble(AppContext
							.getAccountContext().currentUser()
							.getUploadMaxVolume()) * 1024 * 1024).longValue();

					if (size <= max) {
						success();
						return SUCCESS;
					} else {
						this.addActionError("上传的附件太大！");
						return ERROR;
					}
				}

			} else {
				this.addActionError("上传的文件必须是ZIP压缩文件！");
				return ERROR;
			}
		} else {
			this.addActionError("上传的附件不能为空！");
			return ERROR;
		}
	}

	public void success() {
		try {
			unZip(uplodS.getAbsolutePath(), "C://abc//");

			File f = new File("C://abc//");
			File[] fs = f.listFiles();
			List<Rule> rules = new ArrayList<Rule>();
			for (int i = 0; i < fs.length; i++) {
				Rule rule1 = new Rule();
				System.out.println(fs[i].getName());
				if (!fs[i].isDirectory()) {
					rule1.setAttach(FileUtil.getInstance().createAttach(fs[i],
							fs[i].getName()));
					List<FileTypeMaintain> ft = this
							.getFileTypeMaintainService().getFileTypeMaintain(
									rule1.getAttach().getContentType());
					if (ft.size() > 0) {
						for (FileTypeMaintain fileTypeMaintain : ft) {
							rule1.setFileTypeMaintain(fileTypeMaintain);
							rule1.setType("other");
							rule1.setTime(new Date());
							rule1.setSize(rule1.getAttach().getSize());
							rule1.setParent(rule.getParent());
							rule1.setName(fs[i].getName());
							rules.add(rule1);
						}
					} else {
						rule1.setType("others");
						rule1.setTime(new Date());
						rule1.setSize(rule1.getAttach().getSize());
						rule1.setParent(rule.getParent());
						rule1.setName(fs[i].getName());
						rules.add(rule1);
					}
				} else {
					readFile(fs[i]);

				}

			}

			for (Rule r : rules) {
				this.getRuleService().save(r);
			}
			deleteAll(f);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void readFile(File file) throws IOException {
		File[] files = file.listFiles();
		System.out.println(files.length);
		List<Rule> rules = new ArrayList<Rule>();
		for (int i = 0; i < files.length; i++) {
			Rule rule2 = new Rule();
			System.out.println(files[i].getName());
			if (!files[i].isDirectory()) {
				rule2.setAttach(FileUtil.getInstance().createAttach(files[i],
						files[i].getName()));
				List<FileTypeMaintain> ft = this
						.getFileTypeMaintainService()
						.getFileTypeMaintain(rule2.getAttach().getContentType());
				if (ft.size() > 0) {
					for (FileTypeMaintain fileTypeMaintain : ft) {
						rule2.setFileTypeMaintain(fileTypeMaintain);
						rule2.setType("other");
						rule2.setTime(new Date());
						rule2.setSize(rule2.getAttach().getSize());
						rule2.setParent(rule.getParent());
						rule2.setName(files[i].getName());
						rules.add(rule2);
					}
				} else {
					rule2.setType("others");
					rule2.setTime(new Date());
					rule2.setSize(rule2.getAttach().getSize());
					rule2.setParent(rule.getParent());
					rule2.setName(files[i].getName());
					rules.add(rule2);
				}
			} else {
				readFile(files[i]);

			}
		}
		for (Rule r : rules) {
			this.getRuleService().save(r);
		}
	}

	public void deleteAll(File file) {
		File[] fs = file.listFiles();
		for (int i = 0; i < fs.length; i++) {
			if (fs[i].isDirectory()) {
				deleteAll(fs[i]);
			} else {
				fs[i].delete();
			}
		}
		file.delete();
	}

	@SuppressWarnings("unchecked")
	public static void unZip(String fileName, String filePath) throws Exception {
		ZipFile zipFile = new ZipFile(fileName, "GBK"); // 以“GBK”编码创建zip文件，用来处理winRAR压缩的文件。
		Enumeration emu = zipFile.getEntries();

		while (emu.hasMoreElements()) {
			ZipEntry entry = (ZipEntry) emu.nextElement();
			if (entry.isDirectory()) {
				new File(filePath + entry.getName()).mkdirs();
				continue;
			}
			BufferedInputStream bis = new BufferedInputStream(zipFile
					.getInputStream(entry));

			File file = new File(filePath + entry.getName());
			File parent = file.getParentFile();
			if (parent != null && (!parent.exists())) {
				parent.mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(file);
			BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

			byte[] buf = new byte[BUFFER];
			int len = 0;
			while ((len = bis.read(buf, 0, BUFFER)) != -1) {
				fos.write(buf, 0, len);
			}
			bos.flush();
			bos.close();
			bis.close();
		}
		zipFile.close();
	}

	@Action(value = "upload", results = {
			@Result(name = "success", type = "redirect", location = "listRule"),
			@Result(name = "error", type = "chain", location = "editUpload") })
	public String upload() {
		if ("".equals(rule.getName())) {
			this.addActionError("文件主题不能为空！");
			return ERROR;
		}
		if (upload != null) {
			File file = new File(upload.getPath());
			try {
				long sizes = new FileInputStream(file).available();
				if ("0".equals(AppContext.getAccountContext().currentUser()
						.getUploadMaxVolume())
						|| "".equals(AppContext.getAccountContext()
								.currentUser().getUploadMaxVolume())) {
					this.rule.setAttach(FileUtil.getInstance().createAttach(
							upload, uploadFileName));
					List<FileTypeMaintain> ft = this
							.getFileTypeMaintainService().getFileTypeMaintain(
									rule.getAttach().getContentType());
					if (ft.size() > 0) {
						for (FileTypeMaintain fileTypeMaintain : ft) {
							rule.setFileTypeMaintain(fileTypeMaintain);
							rule.setType("other");
						    rule.setTime(new Date());
						}
					} else {
						rule.setType("others");
					}
					rule.setUpPerson(AppContext.getAccountContext().currentUser());
					this.getRuleService().save(rule);
					return SUCCESS;
				} else {
					long max = new Double(Double.parseDouble(AppContext
							.getAccountContext().currentUser()
							.getUploadMaxVolume()) * 1024 * 1024).longValue();
					if (sizes <= max) {
						this.rule.setAttach(FileUtil.getInstance()
								.createAttach(upload, uploadFileName));
						List<FileTypeMaintain> ft = this
								.getFileTypeMaintainService()
								.getFileTypeMaintain(
										rule.getAttach().getContentType());
						if (ft.size() > 0) {
							for (FileTypeMaintain fileTypeMaintain : ft) {
								rule.setFileTypeMaintain(fileTypeMaintain);
								rule.setType("other");
								rule.setTime(new Date());
							}
						} else {
							rule.setType("others");
						}
						rule.setUpPerson(AppContext.getAccountContext().currentUser());
						this.getRuleService().save(rule);
						return SUCCESS;
					} else {
						this.addActionError("上传的附件太大！");
						return ERROR;
					}
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			this.addActionError("上传的附件不能为空！");
			return ERROR;
		}
		rule.setUpPerson(AppContext.getAccountContext().currentUser());
		this.getRuleService().save(rule);
		return SUCCESS;
	}

	@Action("editSearch")
	public String editSearch() {
		return SUCCESS;
	}

	@Action("editVolumeUpload")
	public String editVolumeUpload() {
		return SUCCESS;
	}

	@Action("editQuery")
	public String editQuery() {
		return SUCCESS;
	}

	@Action("editRenameFile")
	public String editRenameFile() {
		return SUCCESS;
	}

	@Action("editRenameCategory")
	public String editRenameCategory() {
		return SUCCESS;
	}

	@Action("listMatch")
	public String listMatch() {
		return SUCCESS;
	}

	@Action(value = "query", results = {
			@Result(name = "success", type = "redirect", location = "listMatch?rule=${rule.getId()}"),
			@Result(name = "error", type = "chain", location = "editQuery") })
	public String query() {
		List<Rule> docs = this.getRuleService().getAllRules();
		int count = 0;
		for (int i = 0; i < docs.size(); i++) {
			if (docs.get(i).getName().equals(rule.getName())) {
				if (docs.get(i).getRecorder().getResume().getName() == rule
						.getRecorder().getResume().getName()) {
					count++;
					this.rule = docs.get(i);
				}
			}
		}
		if (count > 0) {
			System.out.println(rule.getId());
			return SUCCESS;
		} else {
			return ERROR;
		}

	}
}
